/*
 * Author: Le Truong
 * Email: travistrle@gmail.com
 * Date: 15DEC2013
 */

package vn.buffalocoders.rikos.ui.signup;

import java.util.Map;

import vn.buffalocoders.rikos.R;
import vn.buffalocoders.rikos.ui.category.CategoryActivity;
import vn.buffalocoders.rikos.ui.common.Helper;
import vn.buffalocoders.rikos.ui.network.ConnectRikoServer;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings.Secure;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Rect;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class SignUpActivity extends Activity implements OnClickListener{

	private UserRegisterTask mAuthTask = null;
	// UI reference
	private TextView mValidateText;
	private EditText mUserView;
	private EditText mEmailView;
	private EditText mPasswordView;
	private ProgressDialog progress;
	private RelativeLayout root;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sign_up);

		// Set up the login form
		mValidateText = (TextView) findViewById(R.id.validate_text);
		mUserView = (EditText) findViewById(R.id.usernameTxt);
		mEmailView = (EditText) findViewById(R.id.emailTxt);
		mPasswordView = (EditText) findViewById(R.id.passwordTxt);
		root = (RelativeLayout) findViewById(R.id.signup_controls_form);
		
		mValidateText.setVisibility(TextView.INVISIBLE);

		progress = new ProgressDialog(this);
		findViewById(R.id.registerBtn).setOnClickListener(this);
		findViewById(R.id.termOfUseBtn).setOnClickListener(this);
		findViewById(R.id.fbRegisterBtn).setOnClickListener(this);

		root.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
			  @Override public void onGlobalLayout() {
			    Rect r = new Rect();
			    //r will be populated with the coordinates of your view that area still visible.
			    root.getWindowVisibleDisplayFrame(r);
			 
			    int screenHeight = root.getRootView().getHeight();
			    int heightDiff = screenHeight - (r.bottom - r.top);
			    boolean visible = heightDiff > screenHeight / 3;
			 
			    if (visible) {
			    	Log.i("LETRUONG", "Keyboard visible");
			    	// Animation
			    	findViewById(R.id.termOfUseBtn).setVisibility(View.INVISIBLE);
			    	root.setBackgroundColor(Color.parseColor("#aa000000"));
			    }
			    else {
			    	Log.i("LETRUONG", "Keyboard invisible");
			    	findViewById(R.id.termOfUseBtn).setVisibility(View.VISIBLE);
			    	root.setBackgroundColor(Color.parseColor("#00ffffff"));
			    }
			  }
			});
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v.getId() == R.id.registerBtn) {
			attempteSignup();
		}
		else if(v.getId() == R.id.termOfUseBtn) {
			
		}
		else if(v.getId() == R.id.fbRegisterBtn) {
			
		}
	}
	
    public class UserRegisterTask extends AsyncTask<Void /* Param */, Void /* Progress */, Map<String, String> /* Result */> {

		@Override
		protected Map<String, String> doInBackground(Void... params) {
			// TODO Auto-generated method stub
			return ConnectRikoServer.registerWithRikoServer(mUserView.getText().toString(), mEmailView.getText().toString(), mPasswordView.getText().toString(), 
					Secure.getString(getBaseContext().getContentResolver(), Secure.ANDROID_ID), "");
		}
    	
		@Override
		protected void onPostExecute(final Map<String, String> result) {
			mAuthTask = null;
			
			if (result == null) {
				// Validate or Network error
				mValidateText.setVisibility(View.VISIBLE);
				mValidateText.setText(getResources().getString(R.string.error_connection_with_rikos));
			}
			else {
				if (result.get(ConnectRikoServer.RIKO_RESULT_STATUS).equals(ConnectRikoServer.RIKO_STATUS_SUCCESS)) {
					Intent intent = new Intent(SignUpActivity.this, CategoryActivity.class);
					intent.putExtra(ConnectRikoServer.RIKO_RESULT_USER_ID, result.get(ConnectRikoServer.RIKO_RESULT_USER_ID));
					intent.putExtra(ConnectRikoServer.RIKO_RESULT_AUTH_TOKEN, result.get(ConnectRikoServer.RIKO_RESULT_AUTH_TOKEN));
					startActivity(intent);
				}
				else if (result.get(ConnectRikoServer.RIKO_RESULT_STATUS).equals(ConnectRikoServer.RIKO_STATUS_FAILED)) {
					mValidateText.setVisibility(View.VISIBLE);
					mValidateText.setText(result.get(ConnectRikoServer.RIKO_RESULT_CAUSE));
				}
			}
		}
		
		@Override
		protected void onCancelled() {
			mAuthTask = null;
		}
    }
    
	public void attempteSignup() {
		if (mAuthTask != null) {
			return;
		}
		
		if (!Helper.validateForSignUpActivity(getApplicationContext(), mValidateText, mUserView, mEmailView, mPasswordView)) {
			return;
		}
		
		// Send login to Server

		mAuthTask = new UserRegisterTask();
		mAuthTask.execute((Void) null);
	}
	
	private void showProgress() {
		progress.setMessage(String.format("%s.......", getResources().getString(R.string.status_register)));
	    progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
	    progress.setIndeterminate(true);
	    progress.show();
	}
}